import axios from 'axios'

import router from "@/router";
import store from "@/store";
//是否允许跨域携带cookie信息
axios.defaults.withCredentials=false
//
let token=''
axios.defaults.headers.common['Authorization']=token
//
let url='http://127.0.0.1:8093'//如果项目打包时，此为空
let instance=axios.create({
	baseURL:url
})
//请求拦截器：发送请求之前，判断是否存在本地存储token，有添加到hearer内
instance.interceptors.request.use(config=>{
	let token=localStorage.getItem('token')
		config.headers.Authorization="bearer"+token
	return config
},error=>{
	return Promise.reject(error)
})
//响应拦截器：用于拦截后台的报错处理
instance.interceptors.request.use(response=>{
	return response
},err=>{
	 console.log(err.response.status)
	 if (err.response) {
	 	switch(err.response.status){
			case 401:
				console.log('账号密码错误')
				localStorage.setItem('token','')
				this.$router.push('/login')
				break;
			case 403:
				console.log('非法闯入')
				break;
			default:
				console.log('请求错误')
				break;
		}
	 }
	 return Promise.reject(err.response.data)
})

//导出axios实例[没有请求封装时需开启]
export default instance
